//
//  ALPS_MoreRecommendViewController.m
//  Alps
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_MoreRecommendViewController.h"
#import "WSSNetTool.h"
#import "ALPS_FocusModel.h"
#import "ALPS_CarouselFigureTool.h"
#import "ALPS_MainModel.h"
#import "ALPS_ColumnTableViewCell.h"
#import "ALPS_RecommendMoreTableViewCell.h"
#import "ALPS_MoreOtherViewController.h"
#import "MASConstraint+Private.h"
#import "ALPS_ListenViewController.h"
#import "ALPS_TListNLDViewController.h"
#import "ALPS_ClassifyClickClickAlbumController.h"
#import "ALPS_SpecialIntroViewController.h"
#import "ALPS_PlayerMainViewController.h"

@interface ALPS_MoreRecommendViewController ()<UITableViewDelegate, UITableViewDataSource>
/** 主界面 */
@property (nonatomic, strong) UITableView *tableView;
/** 头视图 */
@property (nonatomic, strong) UIView *headerView;
/** 数据源数组 */
@property (nonatomic, strong) NSMutableArray *dataSource;
/** 是否存在头视图 */
@property (nonatomic, assign) BOOL hasHeaderView;
/** 轮播图数组 */
@property (nonatomic, strong) NSMutableArray *focusArray;
/** 头视图下半部分视图 */
@property (nonatomic, strong) UIView *topView;
/** 排行 */
@property (nonatomic, strong) NSDictionary *rankDic;
@end

@implementation ALPS_MoreRecommendViewController

- (void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setMainTableView];
    [self getDataByID:self.categoryId];
    self.rankDic = [[[self.topArray objectAtIndex:0] list] firstObject];
    if (self.categoryId != TALK_SHOW) {
        [self.topArray removeObjectAtIndex:0];        
    }
}

#pragma mark -- 解析数据
- (void)getDataByID:(NSInteger)number
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%ld&contentType=album&device=android&version=4.3.92", number] body:nil headers:nil response:WSSJSON success:^(id result) {
        self.focusArray = [NSMutableArray array];
        //------------------------------头视图----------------------------------//
        //轮播图
        NSDictionary *focusDic = [result objectForKey:@"focusImages"];
        NSArray *focusArr = [focusDic objectForKey:@"list"];
        for (NSDictionary *dic in focusArr) {
            ALPS_FocusModel *model = [[ALPS_FocusModel alloc] initWithDic:dic];
            if (model.type == 2 || model.type == 3 || model.type == 9) {
                [self.focusArray addObject:model];
            }
        }
        if (self.hasHeaderView == NO) {
            ALPS_CarouselFigureTool *tool = [ALPS_CarouselFigureTool direcWithtFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight / 4) ImageArr:self.focusArray AndImageClickBlock:^(NSInteger index) {
                ALPS_FocusModel *model = [self.focusArray objectAtIndex:index];
                switch (model.type) {
                    case 9:{
                        ALPS_SpecialIntroViewController *SIVC = [[ALPS_SpecialIntroViewController alloc] init];
                        SIVC.specialId = model.specialId;
                        [self.navigationController pushViewController:SIVC animated:YES];
                    }
                        break;
                    case 2:{
                        ALPS_ClassifyClickClickAlbumController *CCCAC = [[ALPS_ClassifyClickClickAlbumController alloc] init];
                        CCCAC.albumId = model.albumId;
                        [self.navigationController pushViewController:CCCAC animated:YES];
                    }
                        break;
                    case 3:{
                        ALPS_PlayerMainViewController *PMVC = [[ALPS_PlayerMainViewController alloc] init];
                        PMVC.trackId = model.trackId;
                        [self.navigationController pushViewController:PMVC animated:YES];
                    }
                        break;
                    default:
                        break;
                }
            }];
            [self.headerView addSubview:tool];
            self.hasHeaderView = YES;
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark -- 设置头视图
- (void)setHeaderView
{
    if (self.categoryId != TALK_SHOW) {
        
        self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight / 20 * 7 + 10)];
        self.tableView.tableHeaderView = self.headerView;
        
        self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreeHeight / 4, ScreeWidth, ScreeHeight / 20 * 7 - ScreeHeight / 4)];
        [self.headerView addSubview:_topView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapView:)];
        [self.topView addGestureRecognizer:tap];
        
        NSArray *arr = [[self.topArray objectAtIndex:0] list];
        NSDictionary *dic = [arr firstObject];
        
        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, ScreeHeight / 20 * 7 - ScreeHeight / 4 - 10, ScreeHeight / 20 * 7 - ScreeHeight / 4 - 10)];
        [leftImageView sd_setImageWithURL:[dic objectForKey:@"coverPath"]];
        [self.topView addSubview:leftImageView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreeHeight / 20 * 7 - ScreeHeight / 4, 5, ScreeWidth - ScreeHeight / 20 * 7 + ScreeHeight / 4 - 20, (ScreeHeight / 20 * 7 - ScreeHeight / 4 - 5) / 2)];
        titleLabel.text = [dic objectForKey:@"title"];
        [self.topView addSubview:titleLabel];
        
        UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x, titleLabel.frame.size.height + titleLabel.frame.origin.y - 10, titleLabel.frame.size.width, titleLabel.frame.size.height)];
        detailLabel.font = [UIFont systemFontOfSize:15];
        detailLabel.textColor = [UIColor grayColor];
        detailLabel.text = [dic objectForKey:@"subtitle"];
        [self.topView addSubview:detailLabel];
        
        UILabel *lineLabel = [[UILabel alloc] init];
        lineLabel.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
        [self.headerView addSubview:lineLabel];
        [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(ScreeHeight / 20 * 7, 0, 0, 0));
        }];
    } else {
        self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight / 4 + 10)];
        self.tableView.tableHeaderView = self.headerView;
        
        UILabel *lineLabel = [[UILabel alloc] init];
        lineLabel.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
        [self.headerView addSubview:lineLabel];
        [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(ScreeHeight / 4, 0, 0, 0));
        }];
    }
}

- (void)didTapView:(UITapGestureRecognizer *)tap
{
    ALPS_TListNLDViewController *TLNLDVC = [[ALPS_TListNLDViewController alloc] init];
    TLNLDVC.webKey = [self.rankDic objectForKey:@"key"];
    TLNLDVC.typeJudgment = [self.rankDic objectForKey:@"contentType"];
    [self.navigationController pushViewController:TLNLDVC animated:YES];
}

#pragma mark -- 主界面
- (void)setMainTableView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight - 99) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    [self.tableView registerClass:[ALPS_RecommendMoreTableViewCell class] forCellReuseIdentifier:@"mainCell"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //设置头视图
    [self setHeaderView];
    
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadData];
    }];
    [header setTitle:@"下拉可以刷新..." forState:MJRefreshStateIdle];
    [header setTitle:@"松开即可刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"玩命加载中..." forState:MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.topArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && self.categoryId != TALK_SHOW) {
        return ScreeHeight / 1.02;
    } else {
        return ScreeHeight / 1.9;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_RecommendMoreTableViewCell *cell = [[ALPS_RecommendMoreTableViewCell alloc] init];
    cell.selectionStyle = 0;
    cell.model = [self.topArray objectAtIndex:indexPath.section];
    cell.section = indexPath.section;
    cell.categoryId = self.categoryId;
    
    //跳转页面
    __block ALPS_MoreRecommendViewController *MRVC = self;
    cell.block = ^(NSInteger number)
    {
        ALPS_ClassifyClickClickAlbumController *CCCAC = [[ALPS_ClassifyClickClickAlbumController alloc] init];
        CCCAC.albumId = number;
        [MRVC.navigationController pushViewController:CCCAC animated:YES];
    };
    
    cell.specialBlock = ^(NSInteger number)
    {
        ALPS_SpecialIntroViewController *SIVC = [[ALPS_SpecialIntroViewController alloc] init];
        SIVC.specialId = number;
        [MRVC.navigationController pushViewController:SIVC animated:YES];
    };
    return cell;
}

- (void)reloadData
{
    [self.dataSource removeAllObjects];
    [self getDataByID:self.categoryId];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_MainModel *model = [self.topArray objectAtIndex:indexPath.section];
    if (model.moduleType != 4) {
        ALPS_MoreOtherViewController *MOVC = [[ALPS_MoreOtherViewController alloc] init];
        MOVC.topArray = self.topArray;
        MOVC.whichPage = 2;
        MOVC.indexSection = indexPath.section;
        MOVC.categoryId = self.categoryId;
        [self.navigationController pushViewController:MOVC animated:YES];
    } else {
        ALPS_ListenViewController *LVC = [[ALPS_ListenViewController alloc] init];
        LVC.categoryId = self.categoryId;
        LVC.whichPage = 1;
        [self.navigationController pushViewController:LVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
